package com.inspur.resourcequery.fragment;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.inspur.common.MyApplication;
import com.inspur.resourcequery.AttributeModifyActivity;
import com.inspur.resourcequery.R;
import com.inspur.resourcequery.ResDetailActivity;
import com.inspur.resourcequery.model.ResAttr;
import com.inspur.resourcequery.model.ResData;

public class ResDetailFragment extends Fragment {
	private ResData models;
	public List<ResAttr> resAttrList;
	private TableLayout resDetailLayout = null;
	MyApplication application;
	private boolean ifModifyFlag;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_resdetail, null);
		resDetailLayout = (TableLayout) view.findViewById(R.id.detail_layout);
		final Context context = inflater.getContext();
		ResDetailActivity activity = (ResDetailActivity) context;
		
		models = activity.getModels();
		application = (MyApplication)activity.getApplication();
		//当modifyRes为0时，说明不启用修改权限控制，该模型可修改
		//当modifyRes不为0，且包含本模型的名称时，该模型可修改
		if(application.getModifyRes().equals("0") ||
				(!application.getModifyRes().equals("0") && 
				application.getModifyRes().contains("," + models.getResclassenname() + ","))){
			ifModifyFlag = true;
		} else {
			ifModifyFlag = false;
		}
		// activity.queryDetailData();
		resAttrList = activity.resAttributeList;
		Display display = activity.getWindowManager().getDefaultDisplay();
		
		int width = display.getWidth();
		for (int i = 0; i < resAttrList.size(); i++) {
			final ResAttr obj = resAttrList.get(i);
			TableRow row = new TableRow(activity);
			row.setPadding(5, 5, 3, 5);
			TextView txtView_name = getTextView(activity);
			TextView txtView_value = getTextView(activity);
			
			txtView_name.setWidth((int) ((width) * 0.35));
			txtView_value.setWidth((int) ((width) * 0.55));
//			txtView_name.setTextColor(Color.parseColor("#6381a2"));
			txtView_name.setTextColor(getResources().getColor(R.color.black));
			txtView_value.setTextColor(getResources().getColor(R.color.black));
			txtView_value.setGravity(Gravity.RIGHT);
			
//			txtView_value.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG ); //下划线
			
			txtView_name.setText(obj.getAttributeCnName());
			txtView_value.setText(obj.getAttributeTransValue());

			row.addView(txtView_name);
			row.addView(txtView_value);
			resDetailLayout.addView(row);
			
			View line = new View(activity);
			line.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 1));
			line.setBackgroundColor(Color.parseColor("#e1e8f2"));
			resDetailLayout.addView(line);
			
			//当属性可修改，且具有修改权限时，设置修改链接
			if(obj.getIfModify() == 1 && ifModifyFlag){
				txtView_name.setTextColor(Color.parseColor("#6381a2"));//#436EEE
				txtView_value.setTextColor(Color.parseColor("#6381a2"));
				row.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						if(obj.getParentColumn() != null && !"".equals(obj.getParentColumn().trim())){
							for(ResAttr attr : resAttrList){
								if(attr.getAttributeEnName() != null && obj.getParentColumn().equals(attr.getAttributeEnName())){
									obj.setParentValue(attr.getAttributeValue());
									break;
								}
							}
						}
						Intent intent = new Intent(context, AttributeModifyActivity.class);
						Bundle bundle = new Bundle();
						bundle.putSerializable("attribute", obj);
						bundle.putSerializable("model", models);
						intent.putExtras(bundle);
						startActivityForResult(intent, 100);
					}
				});
			}
		}
		return view;
	}

	private TextView getTextView(Context context) {
		TextView textView = new TextView(context);
		textView.setPadding(10, 5, 5, 5);
		textView.setTextSize(16);
		return textView;
	}
	
}
